package com.douqu.game.main.util;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.ext.*;
import com.douqu.game.core.factory.DatabaseConfig;
import com.douqu.game.core.factory.DatabaseFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.factory.WorldInfoFactory;
import com.douqu.game.core.util.LogUtils;

import java.util.List;
import java.util.Map;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-08-16 10:18
 */
public class FixManager {

    public static void main(String[] args)
    {
//        fixWorld("slg5001");
//        fixWorld("slg5002");
//        fixWorld("slg5003");

        reloadWorld("slg5001");
        reloadWorld("slg5002");
        reloadWorld("slg5003");
    }


    private static void fixPlayer(String db)
    {
        LoadFactory.loadGameObject("data");

        LogUtils.info("修复Player开始 -> ");
        DatabaseConfig databaseConfig = new DatabaseConfig("127.0.0.1", "3306", db, "root", "123456");
        DatabaseFactory databaseFactory = new DatabaseFactory(databaseConfig);
        List<Map<String, Object>> dataList = databaseFactory.query("SELECT * FROM player");

        for(Map<String, Object> data : dataList)
        {
            Player player = new Player();
            player.setObjectIndex(data.get("objectIndex").toString());
            player.setName(data.get("name").toString());
            LogUtils.info("player -> " + player.getObjectIndex());
            byte[] bagByte = (byte[]) data.get("bagInfo");
            LogUtils.info("bagByte -> " + bagByte.length);

            BagInfo bagInfo = new BagInfo(player);
            bagInfo.loadFrom(new ByteBuffer(bagByte));

            byte[] settingByte = (byte[]) data.get("settingInfo");
            LogUtils.info("settingByte -> " + settingByte.length);

            SettingInfo settingInfo = new SettingInfo(player);
            settingInfo.loadFrom(new ByteBuffer(settingByte));

            byte[] challengeByte = (byte[]) data.get("challengeInfo");
            LogUtils.info("challengeByte -> " + challengeByte.length);

            ChallengeInfo challengeInfo = new ChallengeInfo(player);
            challengeInfo.loadFrom(new ByteBuffer(challengeByte));

            byte[] taskByte = (byte[]) data.get("taskInfo");
            LogUtils.info("taskByte -> " + taskByte.length);

            TaskInfo taskInfo = new TaskInfo(player);
            taskInfo.loadFrom(new ByteBuffer(taskByte));

            byte[] boonByte = (byte[]) data.get("boonInfo");
            LogUtils.info("boonByte -> " + boonByte.length);

            BoonInfo boonInfo = new BoonInfo(player);
            boonInfo.loadFrom(new ByteBuffer(boonByte));

            byte[] mailByte = (byte[]) data.get("mailInfo");
            LogUtils.info("mailByte -> " + mailByte.length);

            MailInfo mailInfo = new MailInfo(player);
            mailInfo.loadFrom(new ByteBuffer(mailByte));

            byte[] majorByte = (byte[]) data.get("majorInfo");
            LogUtils.info("majorByte -> " + majorByte.length);

            MajorInfo majorInfo = new MajorInfo(player);
            majorInfo.loadFrom(new ByteBuffer(mailByte));

            LogUtils.info("加载player -> " + player.getName()+ " 成功");

//            ByteBuffer buffer = new ByteBuffer();
//            settingInfo.writeTo(buffer);
//
//            databaseFactory.execute("update player set settingInfo = ? WHERE objectIndex = ?", buffer.getBytes(), player.getObjectIndex());
//
//            LogUtils.info("保存玩家信息 -> " + player.getName() + "  成功");
        }
    }

    private static void reloadPlayer(String db)
    {
        LoadFactory.loadGameObject("data");

        LogUtils.info("重载Player开始 -> ");
        DatabaseConfig databaseConfig = new DatabaseConfig("127.0.0.1", "3306", db, "root", "123456");
        DatabaseFactory databaseFactory = new DatabaseFactory(databaseConfig);
        List<Map<String, Object>> dataList = databaseFactory.query("SELECT * FROM player");

        for(Map<String, Object> data : dataList)
        {
            Player player = new Player();
            player.setObjectIndex(data.get("objectIndex").toString());
            player.setName(data.get("name").toString());
            LogUtils.info("player -> " + player.getObjectIndex());
            byte[] bagByte = (byte[]) data.get("bagInfo");
            LogUtils.info("bagByte -> " + bagByte.length);

            BagInfo bagInfo = new BagInfo(player);
            bagInfo.loadFrom(new ByteBuffer(bagByte));

            byte[] settingByte = (byte[]) data.get("settingInfo");
            LogUtils.info("settingByte -> " + settingByte.length);

            SettingInfo settingInfo = new SettingInfo(player);
            settingInfo.loadFrom(new ByteBuffer(settingByte));

            byte[] challengeByte = (byte[]) data.get("challengeInfo");
            LogUtils.info("challengeByte -> " + challengeByte.length);

            ChallengeInfo challengeInfo = new ChallengeInfo(player);
            challengeInfo.loadFrom(new ByteBuffer(challengeByte));

            byte[] taskByte = (byte[]) data.get("taskInfo");
            LogUtils.info("taskByte -> " + taskByte.length);

            TaskInfo taskInfo = new TaskInfo(player);
            taskInfo.loadFrom(new ByteBuffer(taskByte));

            byte[] boonByte = (byte[]) data.get("boonInfo");
            LogUtils.info("boonByte -> " + boonByte.length);

            BoonInfo boonInfo = new BoonInfo(player);
            boonInfo.loadFrom(new ByteBuffer(boonByte));

            byte[] mailByte = (byte[]) data.get("mailInfo");
            LogUtils.info("mailByte -> " + mailByte.length);

            MailInfo mailInfo = new MailInfo(player);
            mailInfo.loadFrom(new ByteBuffer(mailByte));

            byte[] majorByte = (byte[]) data.get("majorInfo");
            LogUtils.info("majorByte -> " + majorByte.length);

            MajorInfo majorInfo = new MajorInfo(player);
            majorInfo.loadFrom(new ByteBuffer(majorByte));

            LogUtils.info("加载player -> " + player.getName()+ " 成功");
        }
    }


    private static void fixWorld(String db)
    {
        LoadFactory.loadGameObject("data");

        LogUtils.info("修复WorldInfo开始 -> ");
        DatabaseConfig databaseConfig = new DatabaseConfig("127.0.0.1", "3306", db, "root", "123456");
        DatabaseFactory databaseFactory = new DatabaseFactory(databaseConfig);
        List<Map<String, Object>> dataList = databaseFactory.query("SELECT * FROM server");

        Map<String, Object> data = dataList.get(0);
        byte[] worldByte = (byte[]) data.get("worldInfo");
        LogUtils.info("worldInfo -> " + worldByte.length);

        WorldInfoFactory worldInfoFactory = WorldInfoFactory.getInstance();
        worldInfoFactory.loadWorldInfo(worldByte);

        LogUtils.info("加载WorldInfo成功");

        byte[] playerMapByte = (byte[]) data.get("playerWorldMapInfo");
        LogUtils.info("playerWorldMapInfo -> " + playerMapByte.length);

        worldInfoFactory.loadPlayerMapInfo(playerMapByte);

        LogUtils.info("加载playerWorldMapInfo成功");

        LogUtils.info("现在开始保存操作 -> ");

        byte[] newWorldByte = worldInfoFactory.saveWorldInfo();
        byte[] newPlayerMapByte = worldInfoFactory.savePlayerWorldMapInfo();

        LogUtils.info("新的WorldInfo -> " + newWorldByte.length);
        LogUtils.info("新的PlayerWorldMapInfo -> " + newPlayerMapByte.length);

        int result = databaseFactory.execute("update server set worldInfo = ?, playerWorldMapInfo = ?", newWorldByte, newPlayerMapByte);

        LogUtils.info("处理成功 -> " + result);
    }

    private static void reloadWorld(String db)
    {
        LoadFactory.loadGameObject("data");

        LogUtils.info("重新载入WorldInfo开始 -> ");
        DatabaseConfig databaseConfig = new DatabaseConfig("127.0.0.1", "3306", db, "root", "123456");
        DatabaseFactory databaseFactory = new DatabaseFactory(databaseConfig);
        List<Map<String, Object>> dataList = databaseFactory.query("SELECT * FROM server");

        Map<String, Object> data = dataList.get(0);
        byte[] worldByte = (byte[]) data.get("worldInfo");
        LogUtils.info("worldInfo -> " + worldByte.length);

        WorldInfoFactory worldInfoFactory = WorldInfoFactory.getInstance();
        worldInfoFactory.loadWorldInfo(worldByte);

        LogUtils.info("加载WorldInfo成功");

        byte[] playerMapByte = (byte[]) data.get("playerWorldMapInfo");
        LogUtils.info("playerWorldMapInfo -> " + playerMapByte.length);

        worldInfoFactory.loadPlayerMapInfo(playerMapByte);

        LogUtils.info("加载playerWorldMapInfo成功");
    }

    private static void loadWorld(String host, String db, String user, String password)
    {
        LoadFactory.loadGameObject("data");

        LogUtils.info("载入WorldInfo开始 -> ");
        DatabaseConfig databaseConfig = new DatabaseConfig(host, "3306", db, user, password);
        DatabaseFactory databaseFactory = new DatabaseFactory(databaseConfig);
        List<Map<String, Object>> robotList = databaseFactory.query("SELECT objectIndex,fc FROM player");

        List<Map<String, Object>> dataList = databaseFactory.query("SELECT * FROM server");

        Map<String, Object> data = dataList.get(0);
        byte[] worldByte = (byte[]) data.get("worldInfo");
        LogUtils.info("worldInfo -> " + worldByte.length);

        WorldInfoFactory worldInfoFactory = WorldInfoFactory.getInstance();
        worldInfoFactory.loadWorldInfo(worldByte);

//        for(Map<String, Object> robot : robotList)
//        {
//            worldInfoFactory.getWorldArenaData().addArenaRankInfo(robot.get("objectIndex").toString());
//            worldInfoFactory.getWorldRankData().updateRankInfo(SGCommonProto.E_RANK_TYPE.RANK_TYPE_FC, robot.get("objectIndex").toString(), Integer.parseInt(robot.get("fc").toString()));
//        }

        LogUtils.info("加载WorldInfo成功");

//        byte[] playerMapByte = (byte[]) data.get("playerWorldMapInfo");
//        LogUtils.info("playerWorldMapInfo -> " + playerMapByte.length);
//
//        worldInfoFactory.loadPlayerMapInfo(playerMapByte);
//
//        LogUtils.info("加载playerWorldMapInfo成功");

//        byte[] newByte = worldInfoFactory.saveWorldInfo();
//
//        databaseFactory.execute("UPDATE server SET worldInfo = ?", newByte);
//
//        LogUtils.info("修复成功");

    }

}
